package org.tools.wstt.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.tools.wstt.reference.ApplicationComponent;
import org.tools.wstt.utils.logger.IWsttLogger;
import org.tools.wstt.utils.logger.WsttLoggerFactory;

/**
 * Servlet implementation class AHttpServlet
 * 
 */
public class AHttpServlet extends HttpServlet implements ApplicationComponent {
	private static final long serialVersionUID = 1L;
	protected ServletConfig mConfig;
	protected ServletContext mContext;
	protected IWsttLogger mLogger;

	/**
	 * 
	 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		this.mConfig = config;
		this.mContext = config.getServletContext();
		this.mLogger = WsttLoggerFactory.getLogger(this.getClass());
	}

	/**
	 * 
	 */
	public void destroy() {
		this.mConfig = null;
		this.mContext = null;
		this.mLogger = null;
		super.destroy();
	}

	/**
	 * 
	 */
	public void log(String pMessage) {
		if (this.mLogger != null) {
			this.mLogger.info(pMessage);
		}
		if (mContext != null) {
			mContext.log(pMessage);
		}
	}

	/**
	 * 
	 * @param pMessage
	 * @param pThrowable
	 */
	protected void logError(String pMessage, Throwable pThrowable) {
		StringBuffer vSB = new StringBuffer(pMessage);
		vSB.append(" : ");
		vSB.append(pThrowable.getMessage());
		if (this.mLogger != null) {
			this.mLogger.error(vSB.toString());
		}
		if (mContext != null) {
			mContext.log(vSB.toString());
		}

		if (this.mLogger != null && this.mLogger.isDebugEnabled()) {
			this.mLogger.debug("Details : ", pThrowable);
		}
	}
}
